﻿using System;
using System.Collections.Generic;
using System.Text;
using Fallin.Entities;
using System.IO;
using Fallin.Events;

namespace Fallin.Helpers
{
    public class FileInfoHelper
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(FileInfoHelper));

        public event EventHandler<ProgressEventArgs> Progress;
        public event EventHandler<FolderInfoEventArgs> SubFolderComplete;

        public string FolderPath { get; set; }
        public FolderInfoEntity Result { get; set; }

        public FileInfoHelper(string path)
        {
            FolderPath = path;
        }

        public void Run()
        {
            Result = GetFolderInfo(FolderPath);
        }

        private FolderInfoEntity GetFolderInfo(string path)
        {
            OnProgress(path);

            FolderInfoEntity result = new FolderInfoEntity() { FolderName = path };

            FileInfo info = new FileInfo(path);
            if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                DirectoryInfo[] dirs = new DirectoryInfo[0];
                try
                {
                    dirs = dir.GetDirectories();
                }
                catch (UnauthorizedAccessException ex) { Log.Error(ex.Message); }
                catch (Exception ex) { Log.Error(ex); }

                foreach (var item in dirs)
                {
                    FolderInfoEntity entity = GetFolderInfo(item.FullName);
                    OnSubFolderComplete(entity);

                    result.Size += entity.Size;
                    result.NumOfFiles += entity.NumOfFiles;
                    result.NumOfSubFolders += entity.NumOfSubFolders;
                    result.NumOfSubFolders++;
                }

                FileInfo[] files = new FileInfo[0];
                try
                {
                    files = dir.GetFiles();
                }
                catch (UnauthorizedAccessException ex) { Log.Error(ex.Message); }
                catch (Exception ex) { Log.Error(ex); }

                foreach (var item in files)
                {
                    if (item.Exists)
                    {
                        result.NumOfFiles++;
                        result.Size += item.Length;
                    }
                }
                return result;
            }

            throw new DirectoryNotFoundException();
        }

        protected void OnProgress(string progress)
        {
            if (Progress != null)
                Progress(this, new ProgressEventArgs() { CurrentProgress = progress });
        }

        protected void OnSubFolderComplete(FolderInfoEntity progress)
        {
            if (SubFolderComplete != null)
                SubFolderComplete(this, new FolderInfoEventArgs() { FolderInfo = progress });
        }
    }
}
